package com.handydata.antifraud.ws.dap;

import com.alibaba.fastjson.JSONObject;
import com.handydata.antifraud.annotation.Comment;
import com.handydata.antifraud.common.constant.DataSourceCode;
import com.handydata.antifraud.common.exception.WsInvokeException;
import com.handydata.antifraud.to.dap.HuaDaoScoreReqTO;
import com.handydata.antifraud.to.dap.HuaDaoScoreResTO;
import com.handydata.antifraud.util.DateUtil;
import com.handydata.antifraud.util.encrypt.Md5Utils;
import com.handydata.antifraud.ws.WsInvocationService;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

import java.util.Date;

/**
 * 华道评分
 *
 */
@Slf4j
@Service("huaDaoScoreService")
@Comment("华道评分")
public class HuaDaoScoreServiceImpl extends
		DataPlatformWsService<HuaDaoScoreReqTO, HuaDaoScoreResTO> implements
		WsInvocationService<HuaDaoScoreReqTO, HuaDaoScoreResTO> {

	@Override
	public HuaDaoScoreResTO query(HuaDaoScoreReqTO req) throws WsInvokeException {
		log.info("华道评分查询请求参数 {}", req);
		HuaDaoScoreResTO res = new HuaDaoScoreResTO();
		if (StringUtils.isEmpty(req.getPhoneNum())) {
			log.error("华道评分查询参数校验 {}", "phoneNum为空");
			res.setResCode("1111");
			res.setResMsg("华道评分查询接口phoneNum参数为空");
			return res;
		}
		if (StringUtils.isEmpty(req.getName())) {
			log.error("华道评分查询参数校验 {}", "name为空");
			res.setResCode("1111");
			res.setResMsg("华道评分查询接口name参数为空");
			return res;
		}
		if (StringUtils.isEmpty(req.getIdCard())) {
			log.error("华道评分查询参数校验 {}", "idCard为空");
			res.setResCode("1111");
			res.setResMsg("华道评分查询接口idCard参数为空");
			return res;
		}
		this.dataType = DataSourceCode.HUA_DAO_SCORE.getCode();
		try {
			res = executeQuery(req);
		} catch (Exception e) {
			res.setResCode("1111");
			res.setResMsg("执行外部数据源异常");
		}
		log.info("调用华道评分查询接口返回结果2：{}", JSONObject.toJSONString(res));
		return res;
	}

	@Override
	protected String createQueryXml(HuaDaoScoreReqTO req) {
		StringBuilder xml = new StringBuilder();
		xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
		xml.append("<subatm>");
		xml.append("<application>GwBiz.Req</application>");
		xml.append("<version>2.0.0</version>");
		xml.append("<sendTime>").append(DateUtil.now14()).append("</sendTime>");
		xml.append("<transCode>300657</transCode>");
		xml.append("<channelId>").append(encryInfo.getChannelId()).append("</channelId>");
		xml.append("<channelOrderId>").append(DateUtil.now17()).append("</channelOrderId>");
		xml.append("<cid>").append(Md5Utils.md5ToHexStr(req.getIdCard())).append("</cid>");
		xml.append("<name>").append(Md5Utils.md5ToHexStr(req.getName())).append("</name>");
		xml.append("<phone>").append(Md5Utils.md5ToHexStr(req.getPhoneNum())).append("</phone>");
//		xml.append("<cid>").append(req.getIdCard()).append("</cid>");
//		xml.append("<name>").append(req.getName()).append("</name>");
//		xml.append("<phone>").append(req.getPhoneNum()).append("</phone>");
		xml.append("<type>1,3,4</type>");
		xml.append("</subatm>");
		log.info("华道评分查询调用翰迪数据平台请求报文{}", xml);
		req.setQueryXml(xml.toString());
		return xml.toString();
	}

	@Override
	protected HuaDaoScoreResTO parseResultAndSave(String resultXml,	String orderId, Date createDate) {
		log.info("调用华道评分查询接口返回报文：{}", resultXml);
		HuaDaoScoreResTO resTO = new HuaDaoScoreResTO();
		try {
			// 解析xml，保存本地DB
			JSONObject jasonObject = JSONObject.parseObject(resultXml);
			if (null != jasonObject) {
				if ("0000".equals(jasonObject.get("resCode"))
						&& StringUtils.isNotBlank(jasonObject.getString("resData"))) {
					handResult(jasonObject.getString("resData"), resTO);
				}
				resTO.setResCode((String) jasonObject.get("resCode"));
				resTO.setResMsg((String) jasonObject.get("resMsg"));
			} else {
				resTO.setResCode("0001");
				resTO.setResMsg("华道评分查询接口返回失败");
			}
		} catch (Exception e) {
			log.info("调用华道评分查询接口返回报文解析异常：{}", e);
			resTO.setResCode("1111");
			resTO.setResMsg("调用华道评分查询接口异常");
		}
		log.info("调用华道评分查询接口返回结果1：{}", JSONObject.toJSONString(resTO));
		return resTO;
	}

	@SuppressWarnings("unchecked")
	@PostConstruct
	@Override
	public void register() {
		registerToWsServiceFactory(this, HuaDaoScoreReqTO.class);
	}
	
	/**
	 * 处理返回结果
	 */
	private void handResult (String resData, HuaDaoScoreResTO resTO) {
		JSONObject data = JSONObject.parseObject(resData);
		if (StringUtils.isNotBlank(data.getString("BSCORE"))) {
			double hdHpscore = Double.parseDouble(data.getString("BSCORE"));
			resTO.setHdHpscore(hdHpscore);
		}
		if (StringUtils.isNotBlank(data.getString("ASCORE"))) {
			double hdCreditscore = Double.parseDouble(data.getString("ASCORE"));
			resTO.setHdCreditscore(hdCreditscore);
		}
		if (StringUtils.isNotBlank(data.getString("BLSCORE"))) {
			int hdSpfraud = Integer.parseInt(data.getString("BLSCORE"));
			resTO.setHdSpfraud(hdSpfraud);
		}
		if (StringUtils.isNotBlank(data.getString("SXA1602030"))) {
			double hdDt01001 = Double.parseDouble("0.0".equals(data.getString("SXA1602030")) ? "0" : data.getString("SXA1602030"));
			resTO.setHdDt01001(hdDt01001);
		}
		if (StringUtils.isNotBlank(data.getString("SXA1602127"))) {
			double hdDt01002 = Double.parseDouble(data.getString("SXA1602127"));
			resTO.setHdDt01002(hdDt01002);
		}
		if (StringUtils.isNotBlank(data.getString("SXB0601051"))) {
			double hdDt01003 = Double.parseDouble(data.getString("SXB0601051"));
			resTO.setHdDt01003(hdDt01003);
		}
		if (StringUtils.isNotBlank(data.getString("SXA1601061"))) {
			double hdDt01004 = Double.parseDouble(data.getString("SXA1601061"));
			resTO.setHdDt01004(hdDt01004);
		}
		if (StringUtils.isNotBlank(data.getString("SXA1601065"))) {
			double hdDt01005 = Double.parseDouble(data.getString("SXA1601065"));
			resTO.setHdDt01005(hdDt01005);
		}
		if (StringUtils.isNotBlank(data.getString("SDA1601070"))) {
			double hdDt01006 = Double.parseDouble(data.getString("SDA1601070"));
			resTO.setHdDt01006(hdDt01006);
		}
		if (StringUtils.isNotBlank(data.getString("SDA1601007"))) {
			double hdDt01007 = Double.parseDouble(data.getString("SDA1601007"));
			resTO.setHdDt01007(hdDt01007);
		}
		if (StringUtils.isNotBlank(data.getString("SXA1601027"))) {
			double hdDt01008 = Double.parseDouble(data.getString("SXA1601027"));
			resTO.setHdDt01008(hdDt01008);
		}
		if (StringUtils.isNotBlank(data.getString("SXA1601026"))) {
			double hdDt01009 = Double.parseDouble(data.getString("SXA1601026"));
			resTO.setHdDt01009(hdDt01009);
		}
		if (StringUtils.isNotBlank(data.getString("SDA1601025"))) {
			double hdDt01010 = Double.parseDouble(data.getString("SDA1601025"));
			resTO.setHdDt01010(hdDt01010);
		}
		if (StringUtils.isNotBlank(data.getString("SXA1601055"))) {
			double hdDt01011 = Double.parseDouble(data.getString("SXA1601055"));
			resTO.setHdDt01011(hdDt01011);
		}
		if (StringUtils.isNotBlank(data.getString("SXA1601033"))) {
			double hdDt01012 = Double.parseDouble(data.getString("SXA1601033"));
			resTO.setHdDt01012(hdDt01012);
		}
		if (StringUtils.isNotBlank(data.getString("SXA1601074"))) {
			double hdDt01013 = Double.parseDouble(data.getString("SXA1601074"));
			resTO.setHdDt01013(hdDt01013);
		}
		if (StringUtils.isNotBlank(data.getString("SDB0601082"))) {
			double hdDt01014 = Double.parseDouble(data.getString("SDB0601082"));
			resTO.setHdDt01014(hdDt01014);
		}
		if (StringUtils.isNotBlank(data.getString("SDA1601053"))) {
			double hdDt01015 = Double.parseDouble(data.getString("SDA1601053"));
			resTO.setHdDt01015(hdDt01015);
		}
		if (StringUtils.isNotBlank(data.getString("SDA1601037"))) {
			double hdDt01016 = Double.parseDouble(data.getString("SDA1601037"));
			resTO.setHdDt01016(hdDt01016);
		}
		if (StringUtils.isNotBlank(data.getString("SXA1601066"))) {
			double hdDt01017 = Double.parseDouble(data.getString("SXA1601066"));
			resTO.setHdDt01017(hdDt01017);
		}
		if (StringUtils.isNotBlank(data.getString("SXA1602063"))) {
			double hdDt01018 = Double.parseDouble(data.getString("SXA1602063"));
			resTO.setHdDt01018(hdDt01018);
		}
		if (StringUtils.isNotBlank(data.getString("SDB0601011"))) {
			double hdDt01019 = Double.parseDouble(data.getString("SDB0601011"));
			resTO.setHdDt01019(hdDt01019);
		}
		if (StringUtils.isNotBlank(data.getString("SDB0601073"))) {
			double hdDt01020 = Double.parseDouble(data.getString("SDB0601073"));
			resTO.setHdDt01020(hdDt01020);
		}
		if (StringUtils.isNotBlank(data.getString("SDG0101001"))) {
			double hdDt01021 = Double.parseDouble(data.getString("SDG0101001"));
			resTO.setHdDt01021(hdDt01021);
		}
		if (StringUtils.isNotBlank(data.getString("ZZW1010030"))) {
			double hdDt01022 = Double.parseDouble(data.getString("ZZW1010030"));
			resTO.setHdDt01022(hdDt01022);
		}
		if (StringUtils.isNotBlank(data.getString("SXX2304001"))) {
			double hdDt01023 = Double.parseDouble(data.getString("SXX2304001"));
			resTO.setHdDt01023(hdDt01023);
		}
		if (StringUtils.isNotBlank(data.getString("SXX3403001"))) {
			double hdDt01024 = Double.parseDouble(data.getString("SXX3403001"));
			resTO.setHdDt01024(hdDt01024);
		}
		if (StringUtils.isNotBlank(data.getString("SXX5203001"))) {
			double hdDt01025 = Double.parseDouble(data.getString("SXX5203001"));
			resTO.setHdDt01025(hdDt01025);
		}
		if (StringUtils.isNotBlank(data.getString("SDX1304001"))) {
			double hdDt01026 = Double.parseDouble(data.getString("SDX1304001"));
			resTO.setHdDt01026(hdDt01026);
		}
		if (StringUtils.isNotBlank(data.getString("SXX2604001"))) {
			double hdDt01027 = Double.parseDouble(data.getString("SXX2604001"));
			resTO.setHdDt01027(hdDt01027);
		}
		if (StringUtils.isNotBlank(data.getString("SXX8803001"))) {
			double hdDt01028 = Double.parseDouble(data.getString("SXX8803001"));
			resTO.setHdDt01028(hdDt01028);
		}
		if (StringUtils.isNotBlank(data.getString("SDB0602025"))) {
			double hdDt01029 = Double.parseDouble(data.getString("SDB0602025"));
			resTO.setHdDt01029(hdDt01029);
		}
		if (StringUtils.isNotBlank(data.getString("SXX2104001"))) {
			double hdDt01030 = Double.parseDouble(data.getString("SXX2104001"));
			resTO.setHdDt01030(hdDt01030);
		}
		if (StringUtils.isNotBlank(data.getString("SXB0601019"))) {
			double hdDt01031 = Double.parseDouble(data.getString("SXB0601019"));
			resTO.setHdDt01031(hdDt01031);
		}
		if (StringUtils.isNotBlank(data.getString("SDB0602001"))) {
			double hdDt01032 = Double.parseDouble(data.getString("SDB0602001"));
			resTO.setHdDt01032(hdDt01032);
		}
	}
	
}
